package cn.stu.system.handler;


import cn.stu.common.utils.UserHolder;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 新增填充
     *
     * @param metaObject 元对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        if (UserHolder.getUser() != null) {
            this.setFieldValByName("createId", UserHolder.getUser().getUserId(), metaObject);
            this.setFieldValByName("updateId", UserHolder.getUser().getUserId(), metaObject);
        }

    }


    /**
     * 更新填充
     *
     * @param metaObject 元对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        if (UserHolder.getUser() != null) {
            if (UserHolder.getUser() != null) {
                this.setFieldValByName("updateId", UserHolder.getUser().getUserId(), metaObject);
            }
        }
    }
}
